﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MemberManagement.UserControls
{
    //应用
    //值得一提的是，WinForm并不能直接把用户自定控件往Windows窗体中拖拽，而自动生成实例（ASP.NET是可以直接拖拽的）。那么如果我们需要在应用中使用，只能自己修改Desginer.cs代码了。
    ////先声明：
    //private MemberManagement.PagingControl.Paging paging1;
    //        //然后在InitializeComponent() 方法中实例化：
    //this.paging1 = new CHVM.PagingControl.Paging();
    ////
    //// paging1
    ////
    //this.paging1.CurrentPage = 1;
    //this.paging1.Location = new System.Drawing.Point(3, 347);
    //this.paging1.Name = "paging1";
    //this.paging1.PageCount = 0;
    //this.paging1.PageSize = 50;
    //this.paging1.Size = new System.Drawing.Size(512, 30);
    //this.paging1.TabIndex = 8;
    //this.paging1.TotalCount = 0;
    ////在这里注册事件
    //this.paging1.EventPaging += new CHVM.PagingControl.EventPagingHandler(this.paging1_EventPaging);

    public delegate void EventPagingHandler(EventArgs e);
    public partial class Pager : UserControl
    {
        public Pager()
        {
            InitializeComponent();
            InitComboPageSize();
        }

        public event EventPagingHandler EventPaging;

        private int _pageSize = 50;
        public int PageSize
        {
            get
            {
                return _pageSize;
            }
            set
            {
                if (value > 0)
                {
                    _pageSize = value;
                }
                else
                {
                    _pageSize = 50;
                }
                this.comboPageSize.Text = _pageSize.ToString();
            }
        }
        private int _currentPage = 1;
        /// <summary>
        /// 当前页
        /// </summary>
        public int CurrentPage
        {
            get
            {
                return _currentPage;
            }
            set
            {
                if (value > 0)
                {
                    _currentPage = value;
                }
                else
                {
                    _currentPage = 1;
                }
            }
        }

        private int _totalCount = 0;
        /// <summary>
        /// 总记录数
        /// </summary>
        public int TotalCount
        {
            get
            {
                return _totalCount;
            }
            set
            {
                if (value >= 0)
                {
                    _totalCount = value;
                }
                else
                {
                    _totalCount = 0;
                }
                this.lblTotalCount.Text = this._totalCount.ToString();
                CalculatePageCount();
            }
        }

        private int _pageCount = 0;
        /// <summary>
        /// 页数
        /// </summary>
        public int PageCount
        {
            get
            {
                return _pageCount;
            }
            set
            {
                if (value >= 0)
                {
                    _pageCount = value;
                }
                else
                {
                    _pageCount = 0;
                }
                this.lblPageCount.Text = _pageCount + "";
            }
        }


        /// <summary>
        /// 计算页数
        /// </summary>
        private void CalculatePageCount()
        {
            if (this.TotalCount > 0)
            {
                this.PageCount = Convert.ToInt32(Math.Ceiling(Convert.ToDouble(this.TotalCount) / Convert.ToDouble(this.PageSize)));
            }
            else
            {
                this.PageCount = 0;
            }
        }


        /// <summary>
        /// 数据绑定
        /// </summary>
        public void Bind()
        {
            this.EventPaging?.Invoke(new EventArgs());
            if (this.CurrentPage > this.PageCount)
            {
                this.CurrentPage = this.PageCount;
            }
            this.txtCurPage.Text = this.CurrentPage + "";
            this.lblTotalCount.Text = this.TotalCount + "";
            this.lblPageCount.Text = this.PageCount + "";
            if (this.CurrentPage == 1)
            {
                this.btnPageFirst.Enabled = false;
                this.btnPagePre.Enabled = false;
            }
            else
            {
                this.btnPageFirst.Enabled = true;
                this.btnPagePre.Enabled = true;
            }
            if (this.CurrentPage == this.PageCount)
            {
                this.btnPageNext.Enabled = false;
                this.btnPageLast.Enabled = false;
            }
            else
            {
                this.btnPageNext.Enabled = true;
                this.btnPageLast.Enabled = true;
            }
            if (this.TotalCount == 0)
            {
                this.btnPageFirst.Enabled = false;
                this.btnPagePre.Enabled = false;
                this.btnPageNext.Enabled = false;
                this.btnPageLast.Enabled = false;
            }
        }

        private void btnPageFirst_Click(object sender, EventArgs e)
        {
            this.CurrentPage = 1;
            this.Bind();
        }

        private void btnPagePre_Click(object sender, EventArgs e)
        {
            this.CurrentPage -= 1;
            this.Bind();
        }

        private void btnPageNext_Click(object sender, EventArgs e)
        {
            this.CurrentPage += 1;
            this.Bind();
        }

        private void btnPageLast_Click(object sender, EventArgs e)
        {
            this.CurrentPage = this.PageCount;
            this.Bind();
        }

        /// <summary>
        ///  改变每页条数
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void comboPageSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.PageSize = Convert.ToInt32(comboPageSize.Text);
            this.Bind();
        }

        private void InitComboPageSize()
        {

            this.comboPageSize.Items.AddRange(new object[]
            {
                    "1","10","20","50","100"
            });
            this.comboPageSize.SelectedItem = "50";
        }

    }
}
